package com.jxs.android.ges;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jxs.android.ges.data.NoticeInfo;
import com.jxs.android.weiget.BuLoadingDailog;
import com.jxs.android.ges.R;

public class LaucherSystemNotice extends LoadingMoreAbstractList {

	private final int noticePageSize = 50;
	private NoticeListInfoAdapter adapter;

	@Override
	public void loadingData(int currentPage, boolean showDialog) {
		PAGE_SIZE = noticePageSize;
		getWebServiceData(GesApplication.getUserInfo().getTicket(), Which.NOTICE_PAGE, showDialog, new DataInitListeren() {
			@Override
			public void initData(BuLoadingDailog progressDialog, boolean isShow) {
				progressDialog.dismiss();
			}
		}, PAGE_SIZE, currentPage);
	}

	@Override
	public int getLayout() {
		return R.layout.bu_common_list_no_top_bar;
	}

	@Override
	public BaseAdapter getAdapter(Context context) {
		adapter = new NoticeListInfoAdapter(this);
		return adapter;
	}

	@Override
	public List<NoticeInfo> paserData(SoapObject retMsgObject, Object... params) {
		List<NoticeInfo> noticeListInfos = new ArrayList<NoticeInfo>();
		if (null != retMsgObject) {
			for (int i = 0; i < retMsgObject.getPropertyCount(); i++) {
				SoapObject taskBeanObject = (SoapObject) retMsgObject.getProperty(i);
				NoticeInfo noticeListInfo = new NoticeInfo();
				noticeListInfo.setNotice_id(taskBeanObject.getProperty("notice_id").toString());
				noticeListInfo.setNotice_title(taskBeanObject.getProperty("notice_title").toString());
				noticeListInfo.setNotice_date(taskBeanObject.getProperty("notice_date").toString());
				noticeListInfos.add(noticeListInfo);
			}
		}
		return noticeListInfos;
	}

	@Override
	public void updateList(Message msg) {
		@SuppressWarnings("unchecked")
		List<NoticeInfo> tems = ((List<NoticeInfo>) msg.obj);
		refreshListView.onRefreshComplete();
		empytText.setVisibility(View.GONE);
		if (currentPage == 1) {
			if (tems.size() < 1) {
				empytText.setVisibility(View.VISIBLE);
				loadMoreButton.setText("数据已全部获取完毕");
				loadMoreButton.setEnabled(false);
			}
		}
		if (tems.size() < PAGE_SIZE) {
			loadMoreButton.setText("数据已全部获取完毕");
			loadMoreButton.setEnabled(false);
		} else {
			loadMoreButton.setText("查看更多...");
			currentPage++;
		}
		adapter.addNoticeInfos(tems);
		adapter.notifyDataSetChanged();
	}

	private class NoticeListInfoAdapter extends BaseAdapter {
		private List<NoticeInfo> noticeInfos = new ArrayList<NoticeInfo>();
		private Context context;

		public void addNoticeInfos(List<NoticeInfo> noticeInfos) {
			if (currentPage == 1) {
				this.noticeInfos.clear();
			}
			this.noticeInfos.addAll(noticeInfos);
		}

		public NoticeListInfoAdapter(Context context) {
			this.context = context;
		}

		public int getCount() {
			return noticeInfos.size();
		}

		public NoticeInfo getItem(int position) {
			return noticeInfos.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final NoticeInfo noticeListInfo = noticeInfos.get(position);
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);
				holder = new ViewHolder();
				holder.item_icon = (ImageView) convertView.findViewById(R.id.item_icon);
				holder.title_tv = (TextView) convertView.findViewById(R.id.item_title);
				holder.desc_tv = (TextView) convertView.findViewById(R.id.item_des);
				holder.list_menu = (ImageView) convertView.findViewById(R.id.item_arrow);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.item_icon.setImageResource(R.drawable.icon_notice);
			holder.title_tv.setText(noticeListInfo.getNotice_title());
			holder.desc_tv.setText(noticeListInfo.getNotice_date());

			OnClickListener onClickListener = new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					onclickView(noticeListInfo);
				}
			};

			holder.list_menu.setOnClickListener(onClickListener);
			convertView.setOnClickListener(onClickListener);

			if (position % 2 == 0) {
				convertView.setBackgroundResource(R.drawable.roundlist_body_gray);
			} else {
				convertView.setBackgroundResource(R.drawable.roundlist_body);
			}

			return convertView;
		}

		private void onclickView(NoticeInfo noticeListInfo) {
			Intent intent = new Intent(context, LaucherSystemNoticeDetail.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable("noticeInfo", noticeListInfo);
			intent.putExtras(bundle);
			context.startActivity(intent);
		}

		class ViewHolder {
			TextView title_tv;//
			TextView desc_tv;//
			ImageView list_menu;//
			ImageView item_icon;//
		}

	}

	@Override
	public String getMethod() {
		return "getNoticePage";
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return onKeyDownListener(keyCode, event);
	}
}